<template>
    <div>
        <el-popover
            placement="top"
            width="200"
            v-model="visible">
            <el-select filterable v-model="selectOrgId">
                <el-option
                    v-for="item in orgList"
                    :key="item.organizationId"
                    :label="item.shortName"
                    :value="item.organizationId">
                </el-option>
            </el-select>
            <div style="text-align: right; margin: 10px">
                <el-button size="mini" @click="close">取消</el-button>
                <el-button type="primary" size="mini" @click="bindOrg">确定</el-button>
            </div>
            <el-link slot="reference">{{organizationId == 0 ? '暂无关联业主单位' : MapOrgName(organizationId)}}</el-link>
        </el-popover>
    </div>
</template>
<script>
import { updateOrgIdAndBuild } from '@/api/sysConfig'
export default {
    mame: 'BindOrg',
    props: {
        row: {},
        orgList: {}
    },
    data() {
        return {
            visible: false,
            selectOrgId: this.row.organizationId,
            organizationId: this.row.organizationId
        }
    },
    created() {

    },
    methods: {
        // 映射业主单位名称
        MapOrgName(id) {
            let that = this
            let orgList = that.orgList
            let name = ''
            
            for(var i=0, len=orgList.length; i < len; i++) {
                if(orgList[i].organizationId == id) {
                    name = orgList[i].shortName
                    break
                }
            }
            return name
        },
        close() {
            let that = this
            that.selectOrgId = that.organizationId
            that.visible = false
        },
        // 绑定业主单位
        bindOrg(row) {
            let that = this
            let parames = {
                id: that.row.id,
                organizationId: that.selectOrgId,
                relateObjId: that.row.relateObjId,
                relateObjType: 2,
                type: 1
            }

            updateOrgIdAndBuild(parames).then(res => {
                if(res.status === 200) {
                    that.organizationId = that.selectOrgId
                    that.visible = false
                }
            })
        }
    }
}
</script>
